// 导入组件
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
// 导入组件！
import Layout from '../layout/Index.vue'



// 创建路由匹配的数据集合 -- Array
const routes: Array<RouteRecordRaw> = [
    /*
    {
        path: "/",   // http://192.168.140.1
        name: 'Index',
        component: Index,
    },*/
    {
        path: '/login',
        component: () => import('../views/user/Login.vue'),
        name: 'Login',
    },

    // 首页 --> /dashboard/
    {
        path: '/',
        component: Layout,
        name: 'Layout',
        redirect: '/dashboard',  //自动跳转
        children: [
            {
                path: '/dashboard',
                component: () => import('../views/index/Dashboard.vue'),
                name: 'Dashboard',
                meta: {
                    title: '首页'
                }
            }
        ]
    },

    // 基础数据【院系管理，专业管理，老师管理】
    {
        path: '/basic',
        component: Layout,
        name: 'Basic',
        meta: {
            title: '基础数据'
        },
        children: [
            {
                path: '/basic/branch',
                component: () => import('../views/basic/Branch.vue'),
                name: 'Branch',
                meta: {
                    title: '分部信息'
                },
            },
            {
                path: '/basic/area',
                component: () => import('../views/basic/Area.vue'),
                name: 'Area',
                meta: {
                    title: '业务区信息'
                },
            },
            {
                path: '/basic/place',
                component: () => import('../views/basic/Place.vue'),
                name: 'Place',
                meta: {
                    title: '场地信息'
                },
            },
            {
                path: '/basic/placetype',
                component: () => import('../views/basic/PlaceType.vue'),
                name: 'PlaceType',
                meta: {
                    title: '场地类型'
                },
            },
            {
                path: '/basic/tasktype',
                component: () => import('../views/basic/TaskType.vue'),
                name: 'TaskType',
                meta: {
                    title: '任务类型'
                },
            },
            {
                path: '/basic/taskstatus',
                component: () => import('../views/basic/TaskStatus.vue'),
                name: 'TaskStatus',
                meta: {
                    title: '任务状态'
                },
            },
        ]
    },

    // 学生管理【信息，照片，考试】
    {
        path: '/task',
        component: Layout,
        name: 'Task',
        meta: {
            title: '任务管理',
        },
        children: [
            {
                path: '/task/order',
                component: () => import('../views/task/Order.vue'),
                name: 'Order',
                meta: {
                    title: '任务工单',
                    id: 9,
                },
            },

        ]
    },

    // 用户角色【登陆账号，角色信息，菜单管理，权限管理】
    {
        path: '/user',
        component: Layout,
        name: 'User',
        meta: {
            title: '用户角色',
        },
        children: [
            {
                path: '/user/position',
                component: () => import('../views/user/Position.vue'),
                name: 'Position',
                meta: {
                    title: '职位信息',
                },
            },
            {
                path: '/user/staff',
                component: () => import('../views/user/Staff.vue'),
                name: 'Staff',
                meta: {
                    title: '员工信息',
                },
            },
            {
                path: '/user/roles',
                component: () => import('../views/user/Roles.vue'),
                name: 'Roles',
                meta: {
                    title: '角色信息',
                },
            }, {
                path: '/user/permission',
                component: () => import('../views/user/Permission.vue'),
                name: 'Permission',
                meta: {
                    title: '权限信息',
                },
            },
        ]
    },

]

// 创建一个vue-router的对象 
const router = createRouter({
    history: createWebHistory(),
    routes,
})

// 路由守卫 
router.beforeEach((to, from, next)=>{
    // 判断是否有token, 如果没有token,跳转到 login 
    const token = localStorage.getItem('token')
    // 判断是否有token 是否访问login页面 
    if(!token && to.name !='Login'){
        next({name: 'Login'})
    }
    else next()
})

// 暴露
export default router